﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Threading;
using System.Diagnostics;
using System.IO;

namespace BSM
{
    class Craftbukkit
    {
        
        public TextBox txtRef;
        public Process craftbukkitprocess = new Process();
        public StreamWriter input;
        public StreamReader output;
        public StreamReader error;
        public Thread OutputThread;
        public Thread ErrorThread;
        public void ConsoleWrite(String message)  
        {
            txtRef.Text = txtRef.Text + message;
        }
        public void ConsoleClear()
        {
            txtRef.Text = "";
        }

        internal static void Setup()
        {
            //leving blank for the moment (all code in startserver)
        }
        internal void StartServer()
        {
            #region Startup
            ConsoleClear();
            string otherpath;
            otherpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\BSM";
            string javapath;
            if (Environment.Is64BitOperatingSystem)
            {
                javapath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\java\jre6\bin\java.exe";
            }
            else
            {
                javapath = "java";
            }
            //string javapath = (programpath + @"\java\jre6\bin\java.exe");

            craftbukkitprocess.StartInfo.FileName = "cmd.exe";

            craftbukkitprocess.StartInfo.UseShellExecute = false;
            craftbukkitprocess.StartInfo.ErrorDialog = false;
            craftbukkitprocess.StartInfo.RedirectStandardInput = true;
            craftbukkitprocess.StartInfo.RedirectStandardOutput = true;
            craftbukkitprocess.StartInfo.RedirectStandardError = true;
            craftbukkitprocess.StartInfo.CreateNoWindow = true;
            craftbukkitprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            #endregion
            #region Startserver
            craftbukkitprocess.Start();
            input = craftbukkitprocess.StandardInput;
            output = craftbukkitprocess.StandardOutput;
            error = craftbukkitprocess.StandardError;

            input.WriteLine("cd " + otherpath);
            input.WriteLine(javapath + " -jar craftbukkit.jar nogui");
            OutputThread = new Thread(new ThreadStart(OutputUpdate));
            OutputThread.IsBackground = true;
            OutputThread.Start();
            ErrorThread = new Thread(new ThreadStart(ErrorUpdate));
            ErrorThread.IsBackground = true;
            ErrorThread.Start();
            #endregion
        }
        #region console updater
        void OutputUpdate()
        {

            while (!output.EndOfStream)
            {
                MainWindow.Dispatcher.Invoke((Action)(() =>
                {


                    string text = consolebox.Text;
                    consolebox.Text = text + Environment.NewLine + output.ReadLine();
                    consolebox.ScrollToEnd();
                }));




            }


        }
        void ErrorUpdate()
        {
            while (!error.EndOfStream)
            {
                this.Dispatcher.Invoke((Action)(() =>
                {
                    string text = consolebox.Text;
                    consolebox.Text = text + Environment.NewLine + error.ReadLine();
                    consolebox.ScrollToEnd();
                }));
            }
        }
        #endregion
    }
}
